import { Button } from "antd";
import DetailDrawer_form from "./patient_synthesis_form";
import PatientSynthesisTabs from "./patient_synthesis_tabs";
import React, { useEffect, useState } from "react";
import { useDrawer } from "@/components/Drawer";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { getPatientsPage_list } from "@/api/components/patientInformation";
import { useTranslation } from "react-i18next";
export default function basicDrawer(props: { register: Fn }) {
	const { t } = useTranslation();
	let [userList, setUserList] = useState({
		patientInfo: [],
		patientFams: [],
		patientJobs: [],
		patientInsurs: [],
		patEmgcontacts: [],
		patientConds: []
	});
	let [tabKey, setTabKey] = useState("0");
	const [patientInfo, setPatientInfo] = useState({ patientId: "", patCardno: "" });
	// 打开二级抽屉
	const showChildrenDrawer = () => {
		openDrawer(true, {
			tabKey,
			patientId: patientInfo.patientId,
			patCardno: patientInfo.patCardno
		});
	};
	const [registerDrawer] = useDrawerInner(props.register, async data => {
		console.log("抽屉接收", data);
		// 第一次点击触发，之后的不触发
		setPatientInfo(data.current);
	});
	const addSuccess = async (patientId: string, patCardno: string) => {
		console.log("patCardno", patCardno);
		getPatientInfo(patientId, patCardno);
	};
	const getPatientInfo = async (id1: string = "", id2: string = "") => {
		const res = await getPatientsPage_list({
			patientId: id1 || patientInfo.patientId,
			patCardno: id2 || patientInfo.patCardno
		});
		let replaceNull = JSON.parse(JSON.stringify(res, (key, value) => (value === null ? "" : value))) || {
			patientInfo: [],
			patientFams: [],
			patientJobs: [],
			patientInsurs: [],
			patEmgcontacts: [],
			patientConds: []
		};
		if (patientInfo.patientId) {
			replaceNull.patientInfo = [patientInfo];
		} else {
			let extractedData: any = {};
			// 遍历一级数据的键值对
			for (let key in replaceNull) {
				if (Object.prototype.hasOwnProperty.call(replaceNull, key) && typeof replaceNull[key] !== "object") {
					// 仅提取不是对象的数据
					extractedData[key] = replaceNull[key];
				}
			}
			if (extractedData.patientId) {
				replaceNull.patientInfo = [extractedData];
			}
		}
		setUserList(replaceNull || {});
	};
	const [drawer_form, { openDrawer }] = useDrawer();
	useEffect(() => {
		(patientInfo.patientId || patientInfo.patCardno) && getPatientInfo();
	}, [patientInfo.patientId, patientInfo.patCardno]);

	return (
		<BasicDrawer headerStyle={{ display: "none" }} size="large" {...props} register={registerDrawer}>
			<div className="basic-drawer-footer" key="basic-drawer-footer" slot="footer" style={{ height: "60px" }}>
				<Button
					key="basic-drawer-footer-btn-add"
					disabled={tabKey == "0" && userList.patientInfo.length !== 0}
					className="basic-drawer-footer-btn"
					onClick={showChildrenDrawer}
					type="primary"
				>
					{t("component.toolbar.add")}
				</Button>
				<DetailDrawer_form
					key="DetailDrawer_form-component"
					register={drawer_form}
					success={addSuccess}
					tabKey={tabKey}
				></DetailDrawer_form>
			</div>
			<PatientSynthesisTabs
				key="PatientSynthesisTabs-component"
				tabKey={tabKey}
				userList={userList}
				setTabKey={setTabKey}
				setUserList={setUserList}
				success={addSuccess}
			/>
		</BasicDrawer>
	);
}
